<template>
  <common-list>
    <common-search slot="search" @search="handleQuery" @reset="resetQuery">
      <el-form
        ref="queryForm"
        :inline="true"
        :model="queryParams"
        @submit.native.prevent
        label-width="65px"
      >
        <el-form-item label="接口" prop="apiUrl">
          <common-input
            :verify="false"
            v-model="queryParams.apiUrl"
            @enter="handleQuery"
            clearable
            :maxlength="256"
          />
        </el-form-item>
        <el-form-item label="账号名" prop="userName">
          <common-input
            :verify="false"
            v-model="queryParams.userName"
            @enter="handleQuery"
            clearable
          />
        </el-form-item>

        <el-form-item label="源IP" prop="sourceIp">
          <common-input
            :verify="false"
            v-model="queryParams.sourceIp"
            @enter="handleQuery"
            clearable
          />
        </el-form-item>
        <el-form-item label="目标IP" prop="targetIp">
          <common-input
            :verify="false"
            v-model="queryParams.targetIp"
            @enter="handleQuery"
            clearable
          />
        </el-form-item>
        <el-form-item label="风险操作" prop="isRisk">
          <el-select v-model="queryParams.isRisk">
            <el-option label="全部" :value="null"></el-option>
            <el-option label="是" value="1"></el-option>
            <el-option label="否" value="0"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="访问时间">
          <el-date-picker
            v-model="date"
            type="datetimerange"
            unlink-panels
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            :default-time="['00:00:00', '23:59:59']"
            :clearable="false"
            :editable="false"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
    </common-search>
    <common-table
      slot="content"
      v-loading="loading"
      :data="list"
      @selection-change="handleSelectionChange"
    >
      <el-table-column
        label="账号名"
        align="left"
        fixed
        prop="userName"
        show-overflow-tooltip
      />
      <el-table-column
        label="源IP"
        align="left"
        prop="sourceIp"
        show-overflow-tooltip
      />
      <el-table-column
        label="目标IP"
        align="left"
        prop="targetIp"
        show-overflow-tooltip
      />
      <el-table-column
        label="接口"
        align="left"
        prop="apiUrl"
        show-overflow-tooltip
      />
      <el-table-column
        label="脆弱性"
        align="left"
        prop="weaknessName"
        show-overflow-tooltip
      />
      <el-table-column label="包含风险操作" align="center" width="150">
        <template slot-scope="scope">
          <el-tag type="success" v-if="scope.row.isRisk === 1">是</el-tag>
          <el-tag type="danger" v-else>否 </el-tag>
        </template>
      </el-table-column>
      <el-table-column
        label="访问时间"
        align="left"
        prop="createTime"
        show-overflow-tooltip
      />

      <el-table-column label="操作" align="left" width="50" fixed="right">
        <template slot-scope="scope">
          <template>
            <el-button
              type="text"
              @click="handleInfo(scope.row)"
              v-hasPermi="['apisecurity:visitLogSource:list']"
            >
              详情
            </el-button>
          </template>
        </template>
      </el-table-column>
    </common-table>

    <pagination
      slot="pagination"
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <Info ref="info" v-if="isinfo" @close="closeDialog" :infoRow="infoRow">
    </Info>
  </common-list>
</template>

<script>
import { list } from "@/api/apisecurity/logAnalysis/index";
import Info from "./components/info";
import dayjs from "dayjs";
import BaseList from "@/mixins/BaseList.vue";
export default {
  components: {
    Info,
  },
  name: "index",
  mixins: [BaseList],
  data() {
    return {
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        startTime: "",
        endTime: "",
        userName: "",
        isRisk: null,
        apiUrl: "",
        targetIp: "",
        sourceIp: "",
        weaknessName: "",
        interfaceDataTag: "",
      },
      isinfo: false,
      infoRow: {},
      total: 0,
      list: [],
      loading: false,
      date: [new Date().getTime() - 24 * 60 * 60 * 1000, new Date().getTime()],
    };
  },
  methods: {
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    resetQuery() {
      this.queryParams = this.$options.data().queryParams;
      this.date = [
        new Date().getTime() - 24 * 60 * 60 * 1000,
        new Date().getTime(),
      ];
    },
    getList() {
      if (this.date && this.date.length > 0) {
        this.queryParams.startTime = dayjs(this.date[0]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
        this.queryParams.endTime = dayjs(this.date[1]).format(
          "YYYY-MM-DD HH:mm:ss"
        );
      } else {
        this.queryParams.startTime = null;
        this.queryParams.endTime = null;
      }
      this.loading = true;
      list(this.queryParams)
        .then((res) => {
          if (res && res.code === 200) {
            this.list = res.rows;
            this.total = res.total;
            this.handleListTips(res);
          } else {
            this.list = [];
            this.total = 0;
          }
        })
        .finally(() => {
          this.loading = false;
        });
    },
    handleInfo(row) {
      this.infoRow = row;
      this.isinfo = true;
    },
    closeDialog(row) {
      this.isinfo = false;
    },
  },

  created() {
    this.getList();
  },
};
</script>
<style lang="scss" scoped></style>
